package de.fhhannover.inform.wari.grp8.spiel;

import de.fhhannover.inform.studienklassen.semester3.GeneralStatics;


/*
 * @author Michael Antemann
 * modifiziert am 19.11. ende(stillrunning) variable hinzugefügt
 */

public class Board extends Thread {
	boolean update = true;

	boolean stillrunning = false;
	int turn = 0;
	boolean mWhosTurn = false; // false SpielerB, true SpielerA
	boolean mTypeA = false; // false Mensch , true KI
	boolean mTypeB = false; // false Mensch , true KI
	String mNameA = "SpielerA";
	String mNameB = "SpielerB";
	int[] mPitsA = new int[6];
	int[] mPitsB = new int[6];
	int[] mPits = new int[12];
	int mWinPitA = 0;
	int mWinPitB = 0;


	public Board(int initial) {
		super();
		initialisation(initial);

	}

	/* prot */void reset(int initial) {
		initialisation(initial);
	}

	private void initialisation(int initial) {
		mWhosTurn = false; // false SpielerB, true SpielerA
		mTypeA = false; // false Mensch , true KI
		mTypeB = false; // false Mensch , true KI
		mNameA = "SpielerA";
		mNameB = "SpielerB";
		mPitsA = new int[6];
		mPitsB = new int[6];
		mPits = new int[12];
		mWinPitA = 0;
		mWinPitB = 0;
		turn = 1;

		for (int i = 0; i < 6; i++) {
			mPitsA[i] = initial;
			mPitsB[i] = initial;
			mWinPitA = 0;
			mWinPitB = 0;
		}
		mPits = GeneralStatics.connectArray(mPitsA, mPitsB);
	}

	void initialisation(int initial, boolean whosTurn, boolean typeA,
			boolean typeB, String nameA, String nameB) {
		mWhosTurn = whosTurn; // false SpielerB, true SpielerA
		mTypeA = typeA; // false Mensch , true KI
		mTypeB = typeB; // false Mensch , true KI
		mNameA = nameA;
		mNameB = nameB;
		mWinPitA = 0;
		mWinPitB = 0;
		mPitsA = new int[6];
		mPitsB = new int[6];
		mPits = new int[12];
		turn = 1;
		for (int i = 0; i < 6; i++) {
			mPitsA[i] = initial;
			mPitsB[i] = initial;
			mWinPitA = 0;
			mWinPitB = 0;
		}
		mPits = GeneralStatics.connectArray(mPitsA, mPitsB);
	}

	/* prot */void nextPlayer() {

		if (mWhosTurn) {
			mWhosTurn = false;
			
		} else {
			mWhosTurn = true;
			
		}
		turn++;
		
	}

	/* prot */int addWinPitA(int anzahl) {
		int tmp = mWinPitA;
		mWinPitA = mWinPitA + anzahl;
		return tmp;

	}

	/* prot */int addWinPitB(int anzahl) {
		int tmp = mWinPitB;
		mWinPitB = mWinPitB + anzahl;
		return tmp;
	}

	
	public boolean getUpdate(){
		return update;
	}
	void setUpdate(boolean b) {
		update = b;
	}

	/* prot */int setPit(boolean wessen, int welche, int wert) {
		if (welche >= 0 && welche < 6 && wert > 0 && wert < 48) {
			int tmp = -1;
			if (wessen) {
				tmp = setPitA(welche, wert);
			} else {
				tmp = setPitB(welche, wert);
			}
			return tmp;
		} else
			// FEHLER
			return -2;

	}

	/* prot */int setPitA(int welche, int wert) {
		int tmp = mPitsA[welche];
		mPitsA[welche] = wert;
		mPits[welche] = wert;
		return tmp;
	}

	/* prot */int setPitB(int welche, int wert) {
		int tmp = mPitsB[welche];
		mPitsB[welche] = wert;
		mPits[welche + 6] = wert;
		return tmp;
	}

	/* prot */int[] setPits(int[] in) {
		if (in.length == 12) {
			int[] tmp = mPits;
			mPits = in;
			mPitsA = GeneralStatics.splitArray(mPits, 0, 5);
			mPitsB = GeneralStatics.splitArray(mPits, 6, 11);
			return tmp;
		}
		return null;
	}

	/* prot */int setPit(int welche, int wert) {
		int tmp = mPits[welche];
		mPits[welche] = wert;
		if (welche < 6 && welche > -1) {
			mPitsA[welche] = wert;
		} else if (welche > 5 && welche < 12) {
			welche = welche - 6;
			mPitsB[welche] = wert;
		}
		return tmp;
	}

	/* prot */int[] setPits(boolean wessen, int[] in) {
		if (in.length <= 6 && in != null) {
			if (wessen) {
				in = setPitsA(in);
				return in;
			} else {
				in = setPitsB(in);
				return in;
			}
		}
		return null;
	}

	/* prot */int[] setPitsA(int[] in) {
		int[] tmp = new int[6];
		if (in.length <= 6 && in != null) {
			tmp = mPitsA;
			mPitsA = in;
			mPits = GeneralStatics.replaceInArray(mPits, mPitsA, 0, 5);
			return tmp;
		} else
			return null;
	}

	/* prot */int[] setPitsB(int[] in) {
		int[] tmp = new int[6];
		if (in.length == 6 && in != null) {
			tmp = mPitsB;
			mPitsB = in;
			mPits = GeneralStatics.replaceInArray(mPits, mPitsB, 6, 11);
			return tmp;
		} else
			return null;
	}

	/* prot */String setName(boolean wessen, String name) {
		if (name != null) {
			String tmp;
			if (wessen) {
				tmp = setNameA(name);
				return tmp;
			} else
				tmp = setNameB(name);
			return tmp;
		} else
			return "";
	}

	/* prot */String setNameA(String name) {
		String tmp;
		if (name != null) {
			tmp = mNameA;
			mNameA = name;
			return tmp;
		} else
			return "";
	}

	/* prot */String setNameB(String name) {
		String tmp;
		if (name != null) {
			tmp = mNameB;
			mNameB = name;
			return tmp;
		} else
			return "";
	}

	/* prot */boolean setTypeA(boolean in) {
		boolean typ = mTypeA;
		mTypeA = in;
		return typ;
	}

	/* prot */boolean setTypeB(boolean in) {
		boolean typ = mTypeB;
		mTypeB = in;
		return typ;
	}

	/* prot */String getName(boolean wessen) {
		if (wessen)
			return mNameA;
		else
			return mNameB;
	}

	/* prot */String getNameA() {
		return mNameA;
	}

	/* prot */String getNameB() {
		return mNameB;
	}

	/* prot */int[] getPits() {
		return mPits;
	}

	/* prot */int[] getPits(boolean wessen) {
		if (wessen)
			return getPitsA();
		else
			return getPitsB();
	}

	/* prot */int[] getPitsA() {
		return mPitsA;
	}

	/* prot */int[] getPitsB() {
		return mPitsB;
	}

	/* prot */int getPit(int welche) {
		return mPits[welche];
	}

	/* prot */int getPit(boolean wessen, int welche) {
		if (wessen)
			return getPitA(welche);
		else
			return getPitB(welche);
	}

	/* prot */int getPitA(int welche) {
		return mPitsA[welche];
	}

	/* prot */int getPitB(int welche) {
		return mPitsB[welche];
	}

	/* prot */int getWinPit(boolean wessen) {
		if (wessen)
			return getWinPitA();
		else
			return getWinPitB();
	}

	/* prot */int getWinPitA() {
		return mWinPitA;
	}

	/* prot */int getWinPitB() {
		return mWinPitB;
	}

	/* prot */boolean getType(boolean wessen) {
		if (wessen)
			return getTypeA();
		else
			return getTypeB();
	}

	/* prot */boolean getTypeA() {
		return mTypeA;
	}

	/* prot */boolean getTypeB() {
		return mTypeB;
	}

	/* prot */boolean setWhoseTurn(boolean anDerReihe) {
		boolean tmp = mWhosTurn;
		mWhosTurn = anDerReihe;
		return tmp;
	}

	/* prot */boolean getWhoseTurn() {
		return mWhosTurn;
	}

	public String toString() {
		return mNameA + ":" + GeneralStatics.intArrayToString(mPitsA) + "\t"
				+ mNameB + ":" + GeneralStatics.intArrayToString(mPitsB)
				+ "\nSiegMuldeA:" + mWinPitA + "\t\t\t\t\tSiegMuldeB:"
				+ mWinPitB + "Dran ist: " + mWhosTurn;
		// return GeneralStatics.intArrayToString(mPitsA);
	}

	/* prot */boolean isStillrunning() {
		return stillrunning;
	}

	/* prot */void setStillrunning(boolean stillrunning) {
		this.stillrunning = stillrunning;
	}

	public String toFString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Zug Nr:" + turn + " Am Zug:");
		if (this.getWhoseTurn())
			sb.append("A");
		else
			sb.append("B");
		sb.append("\n\tB:" + mNameB + "\tTyp:" + this.getTypeB() + "\n");
		sb.append("\t"
				+ GeneralStatics.intArrayToSmartString(GeneralStatics
						.twistArray(mPitsB)) + "\n");
		sb.append(mWinPitB + "\t\t\t\t" + mWinPitA + "\n");
		sb.append("\t" + GeneralStatics.intArrayToSmartString(mPitsA) + "\n");
		sb.append("\t\t\tA:" + mNameA + "\tTyp:" + this.getTypeA() + "\n");
		return sb.toString();
	}

	/* prot */int getTurnCount() {
		return turn;
	}

}
